import java.util.PriorityQueue;
import java.util.Stack;

public class Demo1 {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) {
            return head;
        }
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val != val) {
                cur = cur.next;
            }else {
                cur.next = cur.next.next;
            }
        }
        if(head.val == val) {
            return head.next;
        }
        return head;
    }
    public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode prev = null;
        while(cur.next != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return cur;
    }
    public ListNode middleNode(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode cur = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            cur = cur.next;
        }
        return cur;
    }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode tmpH = new ListNode(-1);
        ListNode tmpQ = tmpH;
        while(list1 != null && list2 != null) {
            if(list1.val < list2.val) {
                tmpH.next = list1;
                list1 = list1.next;
            }else {
                tmpH.next = list2;
                list2 = list2.next;
            }
            tmpH = tmpH.next;
        }
        if(list1 != null) {
            tmpH.next = list1;
        }
        if(list2 != null) {
            tmpH.next = list2;
        }
        return tmpQ.next;
    }
    public ListNode mergeTwoLists2(ListNode list1, ListNode list2) {
        ListNode newH = new ListNode(-1);
        ListNode tmpH = newH;
        while(list1 != null && list2 != null) {
            if(list1.val < list2.val) {
                tmpH.next = list1;
                list1 = list1.next;
            }else{
                tmpH.next = list2;
                list2 = list2.next;
            }
            tmpH = tmpH.next;
        }
        if(list1 != null) {
            tmpH.next = list1;
        }
        if(list2 != null) {
            tmpH.next = list2;
        }
        return newH.next;
    }
    public ListNode partition(ListNode pHead, int x) {
        if(pHead == null || pHead.next == null) {
            return pHead;
        }
        Stack<Integer> stack = new Stack<>();
        ListNode cur = pHead;
        ListNode prev = null;
        while(cur != null) {
            if(cur.val < x) {
                stack.add(cur.val);
                // 删除
                if(cur == pHead) {
                    pHead = pHead.next;
                    cur = pHead;
                }else {
                    cur = cur.next;
                    prev.next = cur;
                }
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        while(!stack.isEmpty()) {
            ListNode tmp = new ListNode(stack.pop());
            tmp.next = pHead;
            pHead = tmp;
        }
        return pHead;
    }
    public ListNode partition2(ListNode head, int x) {
        // write code here
        ListNode cur = head;
        ListNode cur1 = null;
        ListNode cur2 = null;
        ListNode head1 = null;
        ListNode head2 = null;
        while(cur != null) {
            if(cur.val < x) {
                if(head1 == null) {
                    head1 = cur;
                    cur1 = cur;
                }else {
                    cur1.next = cur;
                    cur1 = cur1.next;
                }
            }else {
                if(head2 == null) {
                    head2 = cur;
                    cur2 = cur;
                }else {
                    cur2.next = cur;
                    cur2 = cur2.next;
                }
            }
            cur = cur.next;
        }
        if(head1 == null) {
            return head2;
        }
        if(cur2 != null) {
            cur2.next = null;
        }
        cur1.next = head2;
        return head1;
    }
}
